package com.halachawiki.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.halachawiki.client.datepicker.DatePicker;
import com.halachawiki.client.login.LoginControl;
import com.halachawiki.client.login.LoginService.Status;
import com.halachawiki.client.login.UserAdminControl;
import com.halachawiki.shared.PublicUserInfo;

public class HalachaWiki implements EntryPoint {

	VerticalPanel loginControlContainer = new VerticalPanel();
	VerticalPanel changePasswordControlContainer = new VerticalPanel();
	HorizontalPanel userMenuContainer = new HorizontalPanel();
	LoginControl loginControl;
	
	@Override
	public void onModuleLoad() {
		String path = Window.Location.getPath();
		if (path.endsWith("/Login.html") || path.endsWith("/") || path.isEmpty()) {
			RootPanel p = RootPanel.get("login");
			p.setStyleName("center-page");
			p.add(loginControlContainer);
			p.add(this.userMenuContainer);
			loginControl = new LoginControl(loginControlContainer, new AsyncCallback<PublicUserInfo>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void onSuccess(PublicUserInfo userInfo) {
					if (userInfo.status==Status.OK)
						drawUserMenu(userInfo);
				} });
		}
		if (path.endsWith("/Admin.html")) {
			UserAdminControl adminControl = new UserAdminControl();
			RootPanel p = RootPanel.get("admin");
			p.clear();
			p.add(adminControl.getRoot());
			adminControl.draw();
		}
		if (path.endsWith("/DatePicker.html") || path.endsWith("/DatePickerHe.html")) {
			RootPanel p = RootPanel.get("datepicker");
			p.clear();
			p.add(new DatePicker());
		}
		if (path.endsWith("/UserEditor.html") || path.endsWith("/UserEditorHe.html")) {
			RootPanel p = RootPanel.get("usereditor");
			p.clear();
			p.add(new Authors());
		}
		if (path.endsWith("/MultiString.html")) {
			RootPanel p = RootPanel.get("datepicker");
			p.clear();
			p.add(new MultipleStringInput(null));
		}
	}
	
	private void drawUserMenu(PublicUserInfo userInfo) {
		userMenuContainer.add(new Authors());
		userMenuContainer.add(new UserMenu(userInfo, loginControl));		
	}

}
